package com.woniuxy.adminweb.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.woniuxy.admindao.model.Admin;
import com.woniuxy.adminmodel.dto.AdminDto;
import com.woniuxy.adminmodel.form.*;
import com.woniuxy.adminmodel.param.*;
import com.woniuxy.core.status.AdminStateEnum;
import com.woniuxy.adminservice.service.AdminService;
import com.woniuxy.core.exception.ParameterValidException;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员账号相关接口")
public class  AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     *
     * @param form 新增管理员参数列表
     * @param adminToken 管理员认证token
     * @return 无
     */

    @ApiOperation("新增管理员方法")
    @PostMapping("/add.api")
    public Result<?> add(@RequestBody @Validated AddAdminForm form,@RequestHeader String adminToken){
        Integer addAdminId = MyJwtUtil.getAdminIdByKey(adminToken, "adminId");
        AddAdminParam param = BeanUtil.toBean(form, AddAdminParam.class);
        param.setAddAdminId(addAdminId);
        adminService.add(param);
        return Result.success("新增管理员成功");
    }

    /**
     *
     * @param form 管理员账号登录参数列表
     * @param response 返回响应对象
     * @return 无
     */
    @ApiOperation("管理员账号登录方法")
    @PostMapping("/accountLogin.api")
    public Result<?> login(@RequestBody @Validated AdminAccountLoginForm form,HttpServletResponse response, HttpServletRequest request){
        AdminDto admin = adminService.login(BeanUtil.toBean(form, AdminLoginParam.class));
        Map<String, Object> body = new HashMap<>();
        body.put("adminId",admin.getAdminId());
        String adminToken = MyJwtUtil.createAdminToken(body,240);
        response.setHeader("adminToken", adminToken);
        response.setHeader("Access-Control-Expose-Headers", "adminToken");
        String remoteAddr = request.getRemoteAddr();
        int remotePort = request.getLocalPort();
        String loginAddr = remoteAddr+":"+remotePort;
        Map<String, Object> map = new HashMap<>();
        map.put("adminId",admin.getAdminId());
        map.put("loginAddr",loginAddr);
        map.put("loginTime", DateUtil.formatDateTime(new Date()));
        rabbitTemplate.convertAndSend("admin-login-log",map);
        return Result.success("管理员账号登录成功");
    }

    /**
     *
     * @param form 管理员手机号登录参数列表
     * @param response 返回响应对象
     * @return 无
     */
    @ApiOperation("管理员手机号登录方法")
    @PostMapping("/phoneLogin.api")
    public Result<?> login(@RequestBody @Validated AdminPhoneLoginForm form,HttpServletResponse response,HttpServletRequest request){
        AdminDto admin = adminService.login(BeanUtil.toBean(form, AdminLoginParam.class));
        Map<String, Object> body = new HashMap<>();
        body.put("adminId",admin.getAdminId());
        String adminToken = MyJwtUtil.createAdminToken(body,240);
        response.setHeader("adminToken", adminToken);
        response.setHeader("Access-Control-Expose-Headers", "adminToken");
        String remoteAddr = request.getRemoteAddr();
        int remotePort = request.getLocalPort();
        String loginAddr = remoteAddr+":"+remotePort;
        Map<String, Object> map = new HashMap<>();
        map.put("adminId",admin.getAdminId());
        map.put("loginAddr",loginAddr);
        map.put("loginTime", DateUtil.formatDateTime(new Date()));
        rabbitTemplate.convertAndSend("admin-login-log",map);
        return Result.success("管理员手机号登录成功");
    }

    /**
     *
     * @param form 启用管理员账号参数列表
     * @param adminToken 管理员认证token
     * @return 无
     */
    @ApiOperation("启用管理员账号方法")
    @PostMapping("/enable.api")
    public Result<?> changeState(@RequestBody @Validated EnableAdminForm form,@RequestHeader String adminToken){
        Integer adminSId = MyJwtUtil.getAdminIdByKey(adminToken, "adminId");
        ChangeStateParam param = BeanUtil.toBean(form, ChangeStateParam.class);
        param.setAdminSId(adminSId);
        param.setAdminState(AdminStateEnum.ADMIN_ENABLE.getState());
        adminService.changeState(param);
        return Result.success("启用管理员账号成功");
    }

    /**
     *
     * @param form 禁用管理员账号参数列表
     * @param adminToken 管理员认证token
     * @return 无
     */
    @ApiOperation("禁用管理员账号方法")
    @PostMapping("/disable.api")
    public Result<?> changeState(@RequestBody @Validated DisableAdminForm form,@RequestHeader String adminToken){
        Integer adminSId = MyJwtUtil.getAdminIdByKey(adminToken, "adminId");
        ChangeStateParam param = BeanUtil.toBean(form, ChangeStateParam.class);
        param.setAdminSId(adminSId);
        param.setAdminState(AdminStateEnum.ADMIN_DISABLE.getState());
        adminService.changeState(param);
        return Result.success("禁用管理员账号成功");
    }

    @ApiOperation("删除管理员账号方法")
    @PostMapping("/del.api")
    public Result<?> del(@RequestBody @Validated DelAdminForm form,@RequestHeader String adminToken){
        Integer adminSId = MyJwtUtil.getAdminIdByKey(adminToken, "adminId");
        ChangeStateParam param = BeanUtil.toBean(form, ChangeStateParam.class);
        param.setAdminSId(adminSId);
        param.setAdminState(AdminStateEnum.ADMIN_DEL.getState());
        adminService.changeState(param);
        return Result.success("删除管理员账号成功");
    }

    /**
     *
     * @param form 分页查看管理员账号参数列表
     * @return pageInfo 分页查看结果返回除密码外的管理员账号信息
     */
    @ApiOperation("分页查看管理员账号方法")
    @PostMapping("/byPage.api")
    public Result<PageInfo<AdminDto>> byPage(@RequestBody AdminByPageForm form){
        PageInfo<AdminDto> pageInfo = adminService.byPage(BeanUtil.toBean(form, AdminByPageParam.class));
        return Result.success("分页查看管理员账号成功",pageInfo);
    }

    /**
     *
     * @param form 通过id查找管理员参数列表
     * @return dto 通过id返回除密码外的管理员账号信息
     */

    @ApiOperation("通过id查找管理员方法")
    @PostMapping("/byId.api")
    public Result<AdminDto> byId(@RequestBody @Validated AdminByIdForm form){
        AdminDto dto = adminService.byId(BeanUtil.toBean(form, AdminByIdParam.class));
        return Result.success("通过id查找管理员成功",dto);
    }

    /**
     *
     * @param form 修改管理员密码参数列表
     * @param adminToken 管理员认证token
     * @return 无
     */
    @ApiOperation("修改管理员密码方法")
    @PostMapping("/changePass.api")
    public Result<?> changePass(@RequestBody @Validated ChangePassForm form,@RequestHeader String adminToken){
        Integer adminId = MyJwtUtil.getAdminIdByKey(adminToken, "adminId");
        ChangePassParam param = BeanUtil.toBean(form, ChangePassParam.class);
        param.setAdminId(adminId);
        adminService.changePass(param);
        return Result.success("修改密码成功");
    }
}

